window.main = window.main || {};

main.load = function(callback) {
	var callback = callback || qinglan.Function.emptyFun;
	qinglan.Canvas.World.globalRes.loadImg(qinglan.Canvas.Config.resource.img, function(img, percent, isload) {
		console.log(img.src + ' ' + percent + ' ' + isload);
		percent == 1 && callback();
	});
}

main.init = function() {
	console.log('OK');

	var fsm = qinglan.Canvas.World.fsmManager.getFsm('main');

	var enter = function(msg,fromstate){
		qinglan.Debug.info('Enter!! state: ['+this.id+']  msg: ['+msg+']  fromstate: ['+fromstate+']');
	};
	var leave = function(){
		qinglan.Debug.info('Leave!! state: ['+this.id+']');
	};
	var meg = function(msg){
		qinglan.Debug.info('Msg!! state: ['+this.id+']  msg: ['+msg+']');
	}
	var addTestAdvice = function(ids){
		qinglan.Array.each(ids,function(id){
			fsm.onEnter(id, 'enterTest', enter);
			fsm.onLeave(id, 'leaveTest', leave);
			fsm.onMessage(id, 'leaveTest', meg);
		})
	};
	addTestAdvice(['init','resourceLoad','mainMenu','modeSelect','play','over']);
	
	fsm.onEnter('init','initgame',function(){
		this.fsm.enter('resourceLoad', 'loadResource', false);
	});
	
	fsm.onEnter('resourceLoad','loadResource',function(){
		var self = this;
		main.load(function(){
			self.fsm.enter('mainMenu','enterMenu',false);
		})
	});

	fsm.onEnter('mainMenu','showMainMenu',function(){
		 
	});
	
	fsm.enter('init');
};

main.init();
